home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18182 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.9 KB

  1. Path: ix.netcom.com!news
  2. From: giuliano@ix.netcom.com(Giuliano Carlini)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why don't you use garbage collection
  5. Date: 18 Apr 1996 21:51:20 GMT
  6. Organization: Netcom
  7. Message-ID: <4l6dgo$jne@dfw-ixnews1.ix.netcom.com>
  8. References: <AD94A731966836BF@dialup97-6-14.swipnet.se> <1996Apr16.110526.1846@ittpub>
  9. NNTP-Posting-Host: lbx-ca5-09.ix.netcom.com
  10. X-NETCOM-Date: Thu Apr 18  4:51:20 PM CDT 1996
  11.  
  12. DISCLOSURE: I'm associated with geodesic systems the publisher of Great
  13. Circle - a garbage collection library - and hope to earn royalties from
  14. them. You can check them out at www.geodesic.com.
  15.  
  16. If you would like a non-commercial library, check out Hans Boehm's
  17. garbage collector at ftp://parcftp.xerox.com/pub/gc.
  18.  
  19. MA> In article <AUSTERN.96Apr12095652@isolde.mti.sgi.com>,
  20. MA> austern@isolde.mti.sgi.com (Matt Austern) wrote:
  21. MA>In C++ we're used to the idea that heap-allocated and
  22. stack-allocated
  23. MA>objects have different rules.  It's not so radical to introduce
  24. MA>garbage-collected objects as a third category.
  25.  
  26. WIL> In <1996Apr16.110526.1846@ittpub> wil@ittpub.nl (Wil Evers)
  27. WIL>1. Don't we think C++ is complex enough as it is?  In my
  28. experience, the  
  29. WIL>main obstacle to get people to use C++ is its steep learning curve,
  30. and  
  31. WIL>adding yet a third category of objects wouldn't make things any
  32. easier. 
  33.  
  34. Adding a third class of objects to the language won't happen any time
  35. soon. The standards committee just ain't going to do something they
  36. consider to be "big". As others have noted, Ellis and Detlefs have
  37. proposed such a change to C++. What isn't as well known, is that the
  38. available collectors that I'm aware of provide an abstract base class
  39. which simulates this proposal very closely.
  40.  
  41. Does this make things easier or harder. You decide:
  42.     class C1 : public gc { ... }; // objects of class C1 are collected
  43.     class C2 { ... }; // objects of class C2 are not.
  44. You can also specify on a per allocation basis whether an object is
  45. collected to override the above behavior:
  46.     v = new(NOGC) C1; // allocate a C1 which is not collected
  47.     v = new(GC) C2; // allocate a C2 which is collected.
  48. Personally, I believe the tiny investment in learning the above is
  49. repaid thousands of times over. Learning the above takes 2 minutes.
  50. Finding and fixing a memory leak can take hours.
  51.  
  52. WIL>2. Will the destructors of garbage collected objects be called?
  53.  
  54. If you want to. Both Great Circle and Hans Boehm's collectors can
  55. invoke the destructor on an object by object or class by class basis.
  56. Or, if you prefer, an arbitrary finalization function can be called.
  57.  
  58. WIL>4. If the answer to (2) is YES: when will these destructors be
  59. WIL> called?
  60. When the object isn't reachable from the root set <g>. If you need to,
  61. you can force a collection to occur at any time.
  62.  
  63. WIL>What can we assume about the state of the program when
  64. WIL>the destructor runs?
  65. Finalization occurs after the collector is run. It is therefore safe
  66. to allocate memory during finalization, although I can't see why a
  67. "real" program would have to.
  68.  
  69. WIL >How do we guarantee a shortage of non-memory resources is detected
  70. WIL>by the garbage collector before we get to the point where the
  71. WIL>program can't continue?
  72. Your program runs out of memory, the collector runs and recovers
  73. memory. If it couldn't recover any, your fried. But this is just as
  74. true of malloc; if it can't find memory, your program dies. 
  75.  
  76. In <AD9A9E819668CB68B@dialup100-3-15.swipnet.se> lars.farm@nts.mh.se
  77. (Lars Farm) writes: 
  78. >.. Anyway, this wont happen.
  79. >GC seems not to appeal to the committee members. If it did GC would
  80. shurely
  81. >be in the language by now.
  82.  
  83. While it would be better if it were in the language, it isn't
  84. essential. Libraries which conform to the Detlefs/Ellis proposal exist,
  85. and work fine. If the committee changes there mind at some time,
  86. whatever they do will probably conform to it too. And I bet they will
  87. in the next go 'round. After all, Stroustrup was at first dead set
  88. against GC, and his latest statements support the optional use of it.
  89. Of course, it'll be a few years before anyone will even think of
  90. changing the standard.
  91.  
  92. CLG> In <Dq0p81.F38@undergrad.math.uwaterloo.ca>
  93. CLG> clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves)
  94. CLG> writes:
  95. CLG> ...
  96. CLG> >The "GCTransparent" way only requires that you link with a
  97. special library.
  98. CLG>It looks like Great Circle scans the stack, the registers, and the
  99. static data area looking for pointers to data on the heap. Of course,
  100. at run time
  101. CLG>there's no way to tell what's a pointer and what's not, so it just
  102. assumes
  103. CLG>that every word is a pointer. <snip>
  104. Yep.
  105.  
  106. CLG>This seems like a classic example of making a bug look like
  107. CLG> a feature.
  108. Huh???!!!
  109.  
  110. CLG> I may have code that hold a bunch of random integers. The chance
  111. that some of
  112. CLG>these random integers just happen to look like pointers to
  113. something on the
  114. CLG>heap is pretty high.
  115. The Boehm and Geodesic collectors are quite sophisticated, and employ a
  116. technique called "Blacklisting" to minimize this. But, this still can,
  117. and does, happen. But, over time, as variables' values change, the
  118. falsely retained blocks will be recovered by future collections.
  119.  
  120. The amount of falsely retained memory is typically bounded and small.
  121. That is, it doesn't keep growing and growing. A memory leak does.
  122.  
  123. CLG>The other way of using Great Circle ("GCPointers") uses some form
  124. of
  125. CLG>garbage collecting smart pointers. I have to wonder how well this
  126. works
  127. CLG>with other libraries though.
  128.  
  129. I'm not much of an expert on their GCPointers library.
  130.  
  131. CLG>Even so, the GCPointers idea seems less disagreeable than the leaky
  132. CLG>alternative. I'd really like to know how it could work with
  133. third-party
  134. CLG>libraries though.
  135.  
  136. GCTransparent and the Boehm collector really don't leak - that is
  137. falsely retain - much memory at all. I'd say less than .1% as a guess.
  138. But, if you're really concerned about it, GCPointers is available.
  139.  
  140. CLG>Would it be possible to use STL containers, for example, with
  141. GCPointers?
  142.  
  143. I believe so. But, I could be wrong.
  144.  
  145. g
  146.  
  147.  
  148.